﻿using System.Collections.Generic;

namespace CodingWheels.DataTypes.Core
{
	internal interface IConverter<TUnit>
	{
		double Convert(double value, TUnit fromUnit, TUnit toUnit);
	}

	public abstract class Converter<TUnit> : IConverter<TUnit> where TUnit : Unit
	{
		private readonly  Dictionary<UnitSet, Conversion> _conversionList = new Dictionary<UnitSet, Conversion>();

		public double Convert(double value, TUnit fromUnit, TUnit toUnit)
		{
			if(fromUnit == toUnit)
			{
				return value;
			}

			var unitSet = new UnitSet(fromUnit, toUnit);

			return _conversionList[unitSet].Convert(value, fromUnit, toUnit);
		}

		protected void AddConversion(Conversion<TUnit> conversion)
		{
			_conversionList.Add(conversion.UnitSet, conversion);
		}
	}
}
